﻿using AutoMapper;
using B.S.Procurement.Domain;
using B.S.Procurement.Error;
using B.S.Procurement.Write.Api.Application.Command.ProcurementCommand;
using MediatR;
using SqlSugar;

namespace B.S.Procurement.Write.Api.Application.CommandHander.ProcurementCommandHander
{
    public class CreateProcurementCommandHanders : IRequestHandler<CreateProcurementCommands, ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public CreateProcurementCommandHanders(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public Task<ApiResult<int>> Handle(CreateProcurementCommands request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var model = mapper.Map<ProcurementModel>(request);

            model.Del = false;
            model.personnel = "admin";
            model.Time = DateTime.Now;

            int newProductId = db.Insertable(model).ExecuteReturnIdentity();


            result.Code = ApiEnum.success;
            result.Msg = "添加采购信息成功";
            result.Data = newProductId;


            return Task.FromResult(result);
        }
    }
}
